package com.sky.controller.category;

import com.github.pagehelper.Page;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.impl.CategoryServiceImpl;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author iTaotao
 * @date 2023/7/25 17:29
 * @desciption: 菜品分类
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("page")
    public Result<?> queryPage(String name, @RequestParam(defaultValue = "1") String page,
                               @RequestParam(defaultValue = "10") String pageSize, String type) {
        PageResult categoryPage = categoryService.queryPage(name, page, pageSize, type);
        return Result.success(categoryPage);
    }

    /**
     * 添加类别
     *
     * @param category 类别
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping()
    public Result<?> addCategory(@RequestBody Category category) {
        categoryService.addCategory(category);
        return Result.success();

    }

    /**
     * 删除类别id
     *
     * @param id id
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping()
    public Result<?> removeCategoryById(String id) {
        categoryService.removeCategoryById(id);
        return Result.success();
    }

    /**
     * 更新类别id
     *
     * @param category 类别
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping()
    public Result<?> updateCategoryById(@RequestBody Category category) {
        categoryService.updateCategoryById(category);
        return Result.success();

    }

    /**
     * 更新状态通过id
     *
     * @param id     id
     * @param status 状态
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/status/{status}")
    public Result<?> updateStatusById(Long id , @PathVariable Integer status ){
        categoryService.updateStatusById(id , status);
        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<Category>> selectCategoryByType(Integer type){
        List<Category> categoryList = categoryService.selectCategoryByType(type);
        return Result.success(categoryList);

    }
}
